package com.sixmac.service.impl;

import com.sixmac.core.Constant;
import com.sixmac.dao.InfoDao;
import com.sixmac.entity.Info;
import com.sixmac.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2016/5/30 0030 下午 1:53.
 */
@Service
public class InfoServiceImpl implements InfoService {

    @Autowired
    private InfoDao infoDao;

    @Override
    public List<Info> findAll() {
        return infoDao.findAll();
    }

    @Override
    public Page<Info> find(int pageNum, int pageSize) {
        return infoDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<Info> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public Info getById(int id) {
        return infoDao.findOne(id);
    }

    @Override
    public Info deleteById(int id) {
        Info info = getById(id);
        infoDao.delete(info);
        return info;
    }

    @Override
    public Info create(Info info) {
        return infoDao.save(info);
    }

    @Override
    public Info update(Info info) {
        return infoDao.save(info);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public Page<Info> pageByIndex(Integer pageIndex, Integer pageNum, Integer pageSize) {
        return infoDao.pageByIndex(pageIndex, new PageRequest(pageNum - 1, pageSize, Sort.Direction.ASC, "id"));
    }
}